package com.midian.controller.device;

import java.io.File;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;

import com.midian.service.device.IDeviceService;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.midian.common.filter.annotation.Validate;
import com.midian.common.pager.Pager;
import com.midian.common.system.ErrorConstants;
import com.midian.common.system.RetManager;
import com.midian.common.system.SysStatic;
import com.midian.common.utils.CheckUtils;
import com.midian.common.utils.ObjUtils;
import com.midian.controller.user.UserController;
import com.midian.model.device.Device;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 设备号表控制器
 * 
 */
@Controller("deviceController")
public class DeviceController {
	/** 日志 */
	private static Logger LOG = Logger.getLogger(DeviceController.class);

	/** 设备号表业务 */
	private IDeviceService deviceService;

	@Resource(name = "deviceService")
	public void setDeviceService(IDeviceService deviceService) {
		this.deviceService = deviceService;
	}

	/**
	 * 添加设备
	 * 
	 * @param client_key
	 * @param device_token
	 *            设备号
	 * @param user_id
	 *            用户id
	 * @param access_token
	 * @return
	 */
	@Validate(retCode = RetManager.common_error)
	@RequestMapping(value = "/save_device", method = RequestMethod.POST)
	public @ResponseBody
	Object saveDevice(String client_key, String device_token, @RequestParam(required = false) String user_id, @RequestParam(required = false) String access_token) throws Exception {
		JSONObject json = new JSONObject();
		try {
			JSONObject contentJson = new JSONObject();
			// 如果用户已登录的情况，处理该用户的设备号
			if (StringUtils.isNotBlank(user_id) && StringUtils.isNotBlank(access_token)) {
				Device device = new Device();
				device.setUserId(user_id); // 设置用户id
				device.setDeviceToken(device_token); // 设置设备号
				device.setFrontType(CheckUtils.getClientKey(client_key));// 设置设备类型
				Device targetDevice = this.deviceService.saveOrUpdateDevice(device);
				contentJson.put("is_receive", SysStatic.NORMALDELFLAG.equals(targetDevice.getIsReceive()) ? "1" : "2"); // 返回会员推送状态
			} else {
				// 如果不是登录的情况，即注销帐号，清空该设备号的记录
				this.deviceService.clearByDeviceToken(device_token);
				contentJson.put("is_receive", "");
			}
			json.put("content", contentJson);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}

		return json;
	}
	
	
	@Validate(retCode = RetManager.common_change_error)
	@RequestMapping(value = "/update_receive_status", method = RequestMethod.POST)
	public @ResponseBody
	Object updateReceiveStatus(String client_key,String user_id, String access_token,String receive_status) throws Exception {
		JSONObject json = new JSONObject();
		try {
//			int receiveStatus = Integer.parseInt(receive_status);
			System.out.println("========receive_status:::"+receive_status);
			if(!"1".equals(receive_status)&!"2".equals(receive_status)){
				RetManager.getInstance().setRetError(json, RetManager.common_invalid_param+":receive_status");
				return json;
			}
			deviceService.updateUserPushReceiveStatus(user_id, Integer.parseInt(receive_status));
			RetManager.getInstance().setRetSuccess(json, RetManager.common_change_success);
		} catch (Exception e) {
			LOG.error(e.getMessage());
			throw e;
		}

		return json;
	}

}
